import cv2
import numpy as np
from PIL import Image

image_paths = []
# 初始化叠加结果
result = None

for i in range(72, 120):
    filename = f'img/A{i:04d}.jpg'
    # img = cv2.imread(filename)
    image_paths.insert(0, filename)

# 创建画布
canvas = None

# 计算每个像素的标准差
# std_image = np.std(image_paths, axis=0)
# # 计算每个像素的权重
# alpha = 1.0 / (std_image + 1e-9)  # 避免除零错误
# # 归一化权重
# alpha /= np.max(alpha)


# 叠加处理
for image_path in image_paths:

    image = cv2.imread(image_path)
    alpha = 1.0 / (i + 1)  # 权重根据时间间隔设置
    beta = 1.0 - alpha

    # 转换数据类型
    image = image.astype(np.float32)
    if canvas is None:
        canvas = np.zeros_like(image, dtype=np.float32)

    # 叠加处理
    cv2.addWeighted(image, alpha, canvas, beta, 0, canvas)

    # 释放图片内存
    del image

# 调整亮度和对比度
canvas = np.clip((canvas * 1.1 + 10), 0, 255).astype(np.uint8)

# 显示结果
cv2.namedWindow("startrails", 0)
cv2.resizeWindow("startrails", 1600, 1200)
cv2.imshow("startrails", canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2.imwrite("startrails.jpg", canvas)
